<?php

namespace app\company\controller\report\subset;

use app\BaseController;
use app\model\DepartmentAdminModel;
use app\model\DepartmentModel;
use app\model\ProxyEvaluationResultModel;
use app\model\RatingScaleTypeModel;
use app\model\SelfEvaluationResultModel;
use app\model\TaskModel;
use app\model\TaskUserResultModel;
use app\model\UnitModel;
use app\model\UserModel;
use app\model\WarningReportRecordModel;
use think\App;

class Selector extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 量表类型选择器
     * @return bool|string
     */
    public function RatingScaleTypeSelector(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'unit_id|单位ID' => 'require'
            ]);
            $where[] = ['unit_id', '=', $param['unit_id']];
            $where[] = ['status','=',1];
            $RatingScaleTypeModel = new RatingScaleTypeModel();
            $data = $RatingScaleTypeModel->field('id,name')->where($where)->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }


    /**
     * 发布人身份选择器
     * @return bool|string
     */
    public function PublisherSelector(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'unit_id|单位ID' => 'require'
            ]);
            $mode = [
                0 => '超级管理员',
                1 => '普通管理员',
                2 => '咨询师',
                3 => '用户'
            ];
            $TaskModel = new TaskModel();
            $uids = $TaskModel->where('unit_id', '=', $param['unit_id'])->column('uid');
            $UserModel = new UserModel();
            $role_mode = $UserModel->where('id', 'in', $uids)->column('role_mode');
            $data = [];
            foreach ($role_mode as $item) {
                $data[] = [
                    'id' => $item,
                    'name' => $mode[$item]
                ];
            }
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 代测人选择器
     * @return bool|string
     */
    public function proxyUserSelector(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'unit_id|单位ID' => 'require'
            ]);
            $ProxyEvaluationResultModel = new ProxyEvaluationResultModel();
            $proxy_uid = $ProxyEvaluationResultModel->where('unit_id','=',$param['unit_id'])->column('proxy_uid');
            $UserModel = new UserModel();
            $data = $UserModel->field('id,name')->where('id', 'in', $proxy_uid)->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 管理选择器
     * @return bool|string
     */
    public function adminSelector(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'unit_id|单位ID' => 'require'
            ]);
            $UserModel = new UserModel();
            $data = $UserModel->where([
                ['role_mode','<',2],
                ['unit_id','=',$param['unit_id']],
            ])->field('id,name')->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 管理选择器(含咨询师)
     * @return bool|string
     */
    public function adminSelector2(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'unit_id|单位ID' => 'require'
            ]);
            $UserModel = new UserModel();
            $data = $UserModel->where([
                ['role_mode','<',3],
                ['unit_id','=',$param['unit_id']],
            ])->field('id,name')->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }


    /**
     * 部门选择器
     * @return bool|string
     */
    public function DepartmentSelector(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'unit_id|单位ID' => 'require'
            ]);
            $where = [];
            $where[] = ['unit_id','=',$param['unit_id']];
            $DepartmentModel = new DepartmentModel();
            $data = $DepartmentModel->where($where)
                ->withCount('users')
                ->withCount('departmentAdmins')
                ->select()->toArray();
            foreach ($data as &$item){
                $getAllSubcategories = getAllSubcategories($data,$item['id']);
                $users_count = array_column($getAllSubcategories,'users_count');
                $users_count = array_sum($users_count);
                $item['users_count'] = $item['users_count'] + $users_count;
            }
            $UnitModel = new UnitModel();
            $unit_name = $UnitModel->where('id','=',$param['unit_id'])->value('name');
            $list[] = [
                'id' => 0,
                'name' => $unit_name,
                'child' => buildTree($data)
            ];
            return success(['list' => $list, 'total' => count($data), 'name' => $unit_name]);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 预警报告-搜索栏
     * @return bool|string
     */
    public function WarningSearch(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'unit_id|单位ID' => 'require'
            ]);
            $WarningReportRecordModel = new WarningReportRecordModel();
            $TaskUserResultModel = new TaskUserResultModel();
            $SelfEvaluationResultModel = new SelfEvaluationResultModel();
            $ProxyEvaluationResultModel = new ProxyEvaluationResultModel();
            $UserModel = new UserModel();
            $WarningReportList = $WarningReportRecordModel->where('unit_id', '=', $param['unit_id'])->select()->toArray();
            $task_id = [];
            $self_id = [];
            $proxy_id = [];
            foreach ($WarningReportList as $item) {
                switch ($item['source']) {
                    case 1:
                        $task_id[] = $item['result_id'];
                        break;
                    case 2:
                        $self_id[] = $item['result_id'];
                        break;
                    case 3:
                        $proxy_id[] = $item['result_id'];
                        break;
                }
            }
            $uids = [];
            if (count($task_id)){
                $uid = $TaskUserResultModel->where('id','in',$task_id)->column('uid');
                $uids = array_merge($uids,$uid);
            }
            if (count($self_id)){
                $uid = $SelfEvaluationResultModel->where('id','in',$self_id)->column('uid');
                $uids = array_merge($uids,$uid);
            }
            if (count($proxy_id)){
                $uid = $ProxyEvaluationResultModel->where('id','in',$proxy_id)->column('uid');
                $uids = array_merge($uids,$uid);
            }
            $where = [];
            if (count($uids)){
                $uids = array_unique($uids);
                $where[] = ['id','in',$uids];
            }else{
                $where[] = ['id','=',-1];
            }
            $department_id = $UserModel->where($where)->column('department_id');
            $department_id = array_unique($department_id);
            $DepartmentModel = new DepartmentModel();
            $data['department'] = $DepartmentModel->where('id', 'in', $department_id)->select()->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }
}